package com.vitabuybuy.tv.ui.mine.collect

import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.ext.setOnclickNoRepeat
import com.library.common.net.loadingtype.LoadingType
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityCollectionBinding
import com.vitabuybuy.tv.dialog.SpecSelectorDialog
import com.library.common.toast.XToast
import com.library.common.widget.RecycleViewDivider
import com.vitabuybuy.tv.ui.mine.adapter.CollectionCommodityListAdapter
import com.vitabuybuy.tv.ui.mine.collect.viewmodel.CollectionViewModel
import com.vitabuybuy.tv.ui.shopcenter.GoodsDetailsActivity
import kotlinx.android.synthetic.main.activity_collection.*
import kotlinx.android.synthetic.main.activity_collection.refresh

class CollectionActivity : BaseActivity<CollectionViewModel, ActivityCollectionBinding>(){
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_collection

    private val collectAdapter by lazy(LazyThreadSafetyMode.NONE) { CollectionCommodityListAdapter() }
    private val specDialog by lazy { SpecSelectorDialog(mContext) }

    private var pageNumber = 1

    private var isSelectAll = false

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.my_collection))
        mBinding?.viewModel = viewModel
        specDialog.setBottomBtn(SpecSelectorDialog.TYPE_BY_ONLY_AND_TO_CART)
        mTvRight.run {
            text = getString(R.string.management)
            setTextColor(ContextCompat.getColor(mContext, R.color.mainColor))
            visibility = View.VISIBLE
        }
        rcvCommodity.run {
            adapter = collectAdapter
            layoutManager = LinearLayoutManager(mContext)
            addItemDecoration(RecycleViewDivider(mContext, 1))
        }
    }

    override fun observeData() {
        viewModel.getMyCollectList("product", pageNumber)
        viewModel.collectlist.observe(this, Observer {
            if (pageNumber == 1) {
                collectAdapter.setNewData(it)
            } else {
                collectAdapter.addData(it)
            }
            if (collectAdapter.mData.isEmpty()) {
                viewModel.statusView.set(LoadingType.SHOW_EMPTY)
            }else{
                viewModel.statusView.set(LoadingType.SHOW_CONTENT)
            }
            refresh.onLoad(it.size)
        })
    }

    override fun eventHandling() {
        setOnclickNoRepeat(ivChoose, btnDelete){v->
            when (v) {
                ivChoose -> {
                    if (isSelectAll)
                        ivChoose.setImageResource(R.drawable.icon_common_choose_n)
                    else
                        ivChoose.setImageResource(R.drawable.icon_common_choose_s)
                    isSelectAll = !isSelectAll
                    for (bean in collectAdapter.mData) {
                        bean.isChecked = isSelectAll
                    }
                    collectAdapter.notifyDataSetChanged()
                }
                btnDelete -> {
                    var idList = ArrayList<Int>()
                    for (bean in collectAdapter.mData) {
                        if (bean.isChecked)
                            idList.add(bean.id)
                    }
                    if (idList.size == 0) return@setOnclickNoRepeat
                    viewModel.collectOrcancelMore(idList)
                }
            }
        }
        collectAdapter.cancelCollect = {
            viewModel.collectOrcancel(collectAdapter.mData[it].product.id, it, true)
        }
        collectAdapter.chooseCollectItem = {
            collectAdapter.mData[it].isChecked = !collectAdapter.mData[it].isChecked
            collectAdapter.notifyItemChanged(it)
            var checkNum = 0
            for (bean in collectAdapter.mData) {
                if (bean.isChecked) checkNum++
            }
            isSelectAll = if (checkNum == collectAdapter.mData.size) {
                ivChoose.setImageResource(R.drawable.icon_common_choose_s)
                true
            } else {
                ivChoose.setImageResource(R.drawable.icon_common_choose_n)
                false
            }
        }
        collectAdapter.addToCart = {
            specDialog.setSpecDatas(
                collectAdapter.mData[it].product.attributeList,
                collectAdapter.mData[it].product.goodsList,
                collectAdapter.mData[it].product.salePrice,
                collectAdapter.mData[it].product.thumbnail
            )
            specDialog.onAddToCartClick = {goodId: Int, buyNum: Int ->
                viewModel.addToCart(goodId, buyNum)
            }
            specDialog.show()
        }
        refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber++
                viewModel.getMyCollectList("product", pageNumber)
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber = 1
                viewModel.getMyCollectList("product", pageNumber)
            }
        })
        mTvRight.setOnClickListener {
            if (mTvRight.text.toString() == getString(R.string.management)) {
                mTvRight.text = getString(R.string.cancel)
                collectAdapter.isEdit = true
                rlBottom.visibility = View.VISIBLE
            } else {
                collectAdapter.isEdit = false
                mTvRight.text = getString(R.string.management)
                rlBottom.visibility = View.GONE
            }
            collectAdapter.notifyDataSetChanged()
        }
        collectAdapter.itemClickListener = {
            GoodsDetailsActivity.openActivity(mContext,it)
        }
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            0 -> {
                pageNumber = 1
                viewModel.getMyCollectList("product", pageNumber)
                mTvRight.text = getString(R.string.management)
                isSelectAll = false
                collectAdapter.isEdit = false
                collectAdapter.notifyDataSetChanged()
                rlBottom.visibility = View.GONE
            }
            1 -> {
                collectAdapter.removeAt(msg.arg1)
                XToast.success(getString(R.string.cancel_collect_follow))
            }
            2->{
                XToast.success(getString(R.string.already_add_to_cart))
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>) {
            mContext.startActivity(null, CollectionActivity::class.java)
        }
    }

}
